﻿using System;
using System.Text;
using System.Text.RegularExpressions;

namespace XiTi
{
    public class JsonHelper
    {

        public static String question2Html(Question q, string idx = "")
        {
            string s = _question2Html(q, idx);
            s = s.Replace("<span class=\"filling\" ></span>", "________________");
            return Regex.Replace(s, "\\{#blank#\\}\\d*\\{#/blank#\\}", "________________");
        }

        private static string _question2Html(Question qq, string idx = "")
        {
            StringBuilder t = new StringBuilder();
            t.Append("<p id='qdiv' class='question'>\n");

            t.Append("<p class='question_text'>").Append(idx).Append(qq.question_text).Append("</p>\n");

            if (qq.options != null)
            {
                t.Append("<p class='options'>\n");
                foreach (String k in qq.options.Keys)
                {
                    t.Append("<p class='option_item'>" + k + ". " + qq.options[k] + "</p>\n");
                }
                t.Append("</p>\n");
            }

            if (qq.list != null)
            {
                t.Append("<p class='child_question'>\n");
                int i = 0;
                foreach (var q in qq.list)
                {
                    i++;
                    t.Append("<p class='child_item'>").Append(_question2Html(q, "(" + i.ToString() + ")")).Append("</p>\n");
                }
                t.Append("</p>\n");
            }

            t.Append("</p>\n");

            return t.ToString();
        }
    }
}
